const express = require("express");
const db = require("../../utils/db");
const router = express.Router();

// 获取数据
router.post("/c_getDb", (req, res) => {
  const sql = `select * from patient_table`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 1, msg: "请求错误" });
    } else {
      res.send({ code: 0, data, msg: "查询成功" });
    }
  });
});
// 添加
router.post("/c_addDb", (req, res) => {
  const {
    user_id,
    name,
    sex,
    age,
    id_card,
    phoneNum,
    type,
    home_address,
    now_place,
    registrant_date,
    registrant,
  } = req.body;
  const sql = `INSERT INTO patient_table(user_id,NAME,sex,age,id_card,phoneNum,TYPE,home_address,now_place,registrant_date,registrant) 
VALUES('${user_id}','${name}','${sex}','${age}','${id_card}','${phoneNum}','${type}','${home_address}','${now_place}','${registrant_date}','${registrant}')`;
  const sql2=`INSERT INTO visits(user_id) VALUES ('${user_id}')`
  const sql3=`INSERT INTO dangerous(user_id) VALUES ('${user_id}')`
  const sql4=`INSERT INTO infect(user_id) VALUES ('${user_id}')`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 1, msg: "请求错误" });
    } else {
      db.query(sql2,(err2,data2)=>{
        if(err2){
          res.send({ code: 1, msg: "请求错误" });
        }else{
          db.query(sql3,(err3,data3)=>{
            if(err3){
              res.send({ code: 1, msg: "请求错误" });
            }else{
              db.query(sql4,(err4,data4)=>{
                if(err4){
                  res.send({ code: 1, msg: "请求错误" });
                }else{
                  res.send({ code: 0, msg: "添加成功" });
                }
              })
            }
          })
        }
      })
    }
  });
});
// 删除
router.post("/c_delDb", (req, res) => {
  const { user_id } = req.body;
  const sql = `DELETE FROM patient_table WHERE user_id=${user_id}`;
  const sql2 = `DELETE FROM visits WHERE user_id=${user_id}`;
  const sql3 = `DELETE FROM dangerous WHERE user_id=${user_id}`
  const sql4 = `DELETE FROM infect WHERE user_id=${user_id}`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 1, msg: "请求错误" });
    } else {
      db.query(sql2,(err2,data2)=>{
        if(err2){
          res.send({ code: 1, msg: "请求错误" });
        }else{
          db.query(sql3,(err3,data3)=>{
            if(err3){
              res.send({err3, code: 1, msg: "请求错误" });
            }else{
              db.query(sql4,(err4,data4)=>{
                if(err4){
                  res.send({err4, code: 1, msg: "请求错误" });
                }else{
                  res.send({ code: 0, msg: "删除成功" });
                }
              })
            }
          })
        }
      })
    }
  });
});

// 修改
router.post("/c_updateDb", (req, res) => {
  const {
    user_id,
    name,
    sex,
    age,
    birth_date,
    id_card,
    phoneNum,
    type,
    contact_type,
    source_case,
    // registrant_date,  registrant_date='2022-12-12',
    registrant,
    occupation,
    work_unit,
    work_address,
    home_environment,
    home_Info,
    vaccine_situation,
    personal_height,
    personal_weight,
    control_date,
    control_situation,
    personal_nationality,
    passport_number,
    immigration_port,
    immigration_date,
    immigration_way,
    home_address,
    now_place,
    overseas_address,
  } = req.body;
  const sql = `UPDATE patient_table SET  
  NAME='${name}',sex='${sex}',age='${age}',birth_date='${birth_date}',id_card='${id_card}',
 phoneNum='${phoneNum}',TYPE='${type}',contact_type='${contact_type}',
 source_case='${source_case}',registrant='${registrant}',
 occupation='${occupation}',work_unit='${work_unit}',work_address='${work_address}',home_address='${home_address}',
 now_place='${now_place}',overseas_address='${overseas_address}',home_environment='${home_environment}',
 home_Info='${home_Info}',vaccine_situation='${vaccine_situation}',personal_height='${personal_height}',personal_weight='${personal_weight}',control_date='${control_date}',
 control_situation='${control_situation}',personal_nationality='${personal_nationality}',passport_number='${passport_number}',
 immigration_port='${immigration_port}',immigration_date='${immigration_date}',immigration_way='${immigration_way}'
 WHERE user_id = '${user_id}'`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 1, msg: "请求错误" });
    } else {
      res.send({ code: 0, msg: "修改成功" });
    }
  });
});
// 查询
router.post("/c_searchDb", (req, res) => {
  const {
    name,
    sex,
    phoneNum,
    id_card,
    home_address,
    now_place,
    type,
    registrant,
    registrant_date,
  } = req.body;
  const sql = `SELECT * FROM patient_table 
          WHERE name LIKE '%${name}%'  AND sex LIKE '%${sex}%'
          AND phoneNum LIKE '%${phoneNum}%'
          AND id_card LIKE '%${id_card}%'
          AND home_address LIKE '%${home_address}%'
          AND now_place LIKE '%${now_place}%'
          AND type LIKE '%${type}%'
          AND registrant LIKE '%${registrant}%'
          AND registrant_date LIKE '%${registrant_date}%'
          `;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 1, msg: "请求出错" });
    } else {
      res.send({ code: 0, data, msg: "成功" });
    }
  });
});

// 发病就诊
router.post("/p_getDb", (req, res) => {
  const { userId } = req.body;
  const sql = `SELECT * FROM visits WHERE user_id='${userId}'`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 1, msg: "发病就诊数据请求出错" });
    } else {
      res.send({ code: 0, data, msg: "发病就诊数据请求成功" });
    }
  });
});
// 编辑就诊
router.post("/p_editDb", (req, res) => {
  const {
    userId,
    onset_date,
    personal_temperature,
    first_symptoms,
    medication_situation,
    visits_situation,
    hospital_results,
    hospital_opinion,
    admission_symptom,
    admission_date,
    discharge_date,
    complication,
    CT_examine_date,
    CT_examine_outcome,
  } = req.body;
  const sql = `UPDATE visits SET  
onset_date='${onset_date}',
personal_temperature='${personal_temperature}',
first_symptoms='${first_symptoms}',
medication_situation='${medication_situation}',
visits_situation='${visits_situation}',
hospital_results='${hospital_results}',
hospital_opinion='${hospital_opinion}',
admission_symptom='${admission_symptom}',
admission_date='${admission_date}',
discharge_date='${discharge_date}',
complication='${complication}',
CT_examine_date='${CT_examine_date}',
CT_examine_outcome='${CT_examine_outcome}'
WHERE user_id = '${userId}'`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 1, msg: "修改失败" });
    } else {
      res.send({ code: 0, msg: "修改成功" });
    }
  });
});

// 危险因素与暴露史
router.post("/e_getDb", (req, res) => {
  const { userId } = req.body;
  const sql = `SELECT * FROM dangerous WHERE user_id='${userId}'`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 1, msg: "危险因素与暴露史请求出错" });
    } else {
      res.send({ code: 0, data, msg: "危险因素与暴露史请求成功" });
    }
  });
});
router.post('/e_editDb',(req,res)=>{
  const {userId,pregnant,pregnant_week,smoking,smoking_count} = req.body
  const sql=`UPDATE dangerous SET  
  pregnant='${pregnant}',
  pregnant_week='${pregnant_week}',
  smoking='${smoking}',
  smoking_count='${smoking_count}'
  WHERE user_id = '${userId}'
  `
  db.query(sql,(err,data)=>{
    if (err) {
      res.send({ code: 1, msg: "请求出错" });
    } else {
      res.send({ code: 0, data, msg: "修改成功" });
    }
  })
})


// 采样检测情况
router.post("/d_getDb", (req, res) => {
  const { userId } = req.body;
  const sql = `SELECT * FROM sampling WHERE user_id='${userId}'`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 1, msg: "请求出错" });
    } else {
      res.send({ code: 0, data, msg: "请求成功" });
    }
  });
})
// 感染来源分析
router.post("/s_getDb", (req, res) => {
  const { userId } = req.body;
  const sql = `SELECT * FROM infect WHERE user_id='${userId}'`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 1, msg: "请求出错" });
    } else {
      res.send({ code: 0, data, msg: "请求成功" });
    }
  });
})
router.post('/s_editDb',(req,res)=>{
  const {userId,text1,text2} = req.body
  const sql=`UPDATE infect SET  
  text1='${text1}',
  text2='${text2}'
  WHERE user_id = '${userId}'
  `
  db.query(sql,(err,data)=>{
    console.log(sql)
    if (err) {
      res.send({ code: 1, msg: "请求出错" });
    } else {
      res.send({ code: 0, data, msg: "修改成功" });
    }
  })
})

module.exports = router;
